/**
 * 
 */
package com.cxy.ch1.threadlocal;

import com.cxy.tools.SleepTools;

/**
 * @ClassName: ThreadLocalUnsafe
 * @Description: TODO(ThreadLocal的线程不安全演示)
 * @author 菜徐鸭
 * @date 2020年4月13日
 *
 */
public class ThreadLocalUnsafe implements Runnable {
	// public static Number number = new Number(0);
	public Number number = new Number(0);

	public void run() {
		// 每个线程计数加一
		number.setNum(number.getNum() + 1);
		// 将其存储到ThreadLocal中
		value.set(number);
		SleepTools.ms(2);
		// 输出num值
		System.out.println(Thread.currentThread().getName() + "=" + value.get().getNum());
	}

	public static ThreadLocal<Number> value = new ThreadLocal<Number>() {
	};

	/**
	 * static变量会导致线程共享
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		for (int i = 0; i < 5; i++) {
			new Thread(new ThreadLocalUnsafe()).start();
		}
	}

	private static class Number {
		public Number(int num) {
			this.num = num;
		}

		private int num;

		public int getNum() {
			return num;
		}

		public void setNum(int num) {
			this.num = num;
		}

		@Override
		public String toString() {
			return "Number [num=" + num + "]";
		}
	}
}
